{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of Don(AI)ldTrump.ipynb",
      "provenance": [],
      "private_outputs": true,
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/M4cs/Don-AI-ld-Trump/blob/master/Don(AI)ldTrump.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KvdrUEeOMJnr",
        "colab_type": "text"
      },
      "source": [
        "## Generating Tweets from Other Tweets Using GPT-2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Uv9GOoWrMUFH",
        "colab_type": "text"
      },
      "source": [
        "Set TF version and import gpt2"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XIGJIb0fKmKy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%tensorflow_version 1.x\n",
        "!pip install -q gpt-2-simple\n",
        "import gpt_2_simple as gpt2"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lQKkyofFMjRG",
        "colab_type": "text"
      },
      "source": [
        "Mount Google Drive to Save/Load Models and Datasets"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6rfY0haQMl29",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "gpt2.mount_gdrive()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W4Jwj5NGM1F6",
        "colab_type": "text"
      },
      "source": [
        "Set Datset Name\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QZ4JRCsLM3CO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "run_name = \"trump_tweets.txt\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iXSjjNdFM-v2",
        "colab_type": "text"
      },
      "source": [
        "Download Intial Model (Only needed if training new model)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fe0lhSciNF8h",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Medium Model\n",
        "gpt2.download_gpt2(model_name='355M')\n",
        "\n",
        "# Smaller Model\n",
        "# gpt2.download_gpt2(model_name='124M')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5-bp6DVMNYen",
        "colab_type": "text"
      },
      "source": [
        "Upload csv file to gdrive so we can import it here"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g6dReMUzfs1J",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!wget https://drive.google.com/uc?export=download&confirm=cYUe&id=1t_H3pqCiGB9fx2dQLbQt01dXP41vhR1R"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-FWikXsbNbTn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "gpt2.copy_file_from_gdrive(run_name)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NMEKC5CpQgY2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "sess = gpt2.start_tf_sess()\n",
        "gpt2.finetune(\n",
        "    sess,\n",
        "    run_name,\n",
        "    steps=-1,\n",
        "    model_name='355M',\n",
        "    run_name=run_name,\n",
        "    restore_from='fresh',\n",
        "    sample_every=500,\n",
        "    sample_length=160,\n",
        "    learning_rate=0.001,\n",
        "    save_every=500,\n",
        "    print_every=100\n",
        ")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wT-dqHYxkrin",
        "colab_type": "text"
      },
      "source": [
        "Save your model to GDrive"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "miEIVsDxkpIt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "gpt2.copy_checkpoint_to_gdrive(run_name='run1')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ylU7Uqy11HUc",
        "colab_type": "text"
      },
      "source": [
        "# Generate Text\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "erzsW4481RNN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "generation = str(gpt2.generate(sess, run_name=run_name, return_as_list=True, temperature=0.85, nsamples=85, batch_size=85, length=250)[0])\n",
        "print(generation)"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}